package com.study.web.controller.app;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.study.app.domain.bo.AppVersionBo;
import com.study.app.domain.vo.AppVersionVo;
import com.study.app.service.IAppVersionService;
import com.study.common.annotation.Log;
import com.study.common.annotation.RepeatSubmit;
import com.study.common.core.controller.BaseController;

import com.study.common.core.domain.PageQuery;
import com.study.common.core.domain.R;
import com.study.common.core.page.TableDataInfo;
import com.study.common.core.validate.AddGroup;
import com.study.common.core.validate.EditGroup;
import com.study.common.enums.BusinessType;
import com.study.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * app版本管理
 *
 * @author magicyo
 * @date 2023-09-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/version")
public class AppVersionController extends BaseController {

    private final IAppVersionService iAppVersionService;

    /**
     * 查询app版本管理列表
     */
    @SaCheckPermission("system:version:list")
    @GetMapping("/list")
    public TableDataInfo<AppVersionVo> list(AppVersionBo bo, PageQuery pageQuery) {
        return iAppVersionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出app版本管理列表
     */
    @SaCheckPermission("system:version:export")
    @Log(title = "app版本管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppVersionBo bo, HttpServletResponse response) {
        List<AppVersionVo> list = iAppVersionService.queryList(bo);
        ExcelUtil.exportExcel(list, "app版本管理", AppVersionVo.class, response);
    }

    /**
     * 获取app版本管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:version:query")
    @GetMapping("/{id}")
    public R<AppVersionVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long id) {
        return R.ok(iAppVersionService.queryById(id));
    }

    /**
     * 新增app版本管理
     */
    @SaCheckPermission("system:version:add")
    @Log(title = "app版本管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppVersionBo bo) {
        return toAjax(iAppVersionService.insertByBo(bo));
    }

    /**
     * 修改app版本管理
     */
    @SaCheckPermission("system:version:edit")
    @Log(title = "app版本管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppVersionBo bo) {
        return toAjax(iAppVersionService.updateByBo(bo));
    }

    /**
     * 删除app版本管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:version:remove")
    @Log(title = "app版本管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iAppVersionService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}